package com.itheima.itheima_health.service.Impl;

import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.exception.BaseException;
import com.itheima.itheima_health.mapper.OrderMapper;
import com.itheima.itheima_health.mapper.WorkloadstatMapper;
import com.itheima.itheima_health.service.WorkloadstatService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkloadstatServiceImpl implements WorkloadstatService {
    @Autowired
    private WorkloadstatMapper workloadstatMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<String> GetRecentrYear(LocalDate now) {
        try{
            //存储每个月份
            List<String> monthList = new ArrayList<>();

            LocalDate begin = now.plusMonths(-12);

            //循环获取每个月并且格式化
            while (!now.equals(begin)){
                String monthTime = begin.getYear()+ "." + begin.getMonthValue();
                monthList.add(monthTime);
                begin = begin.plusMonths(+1);
            }

            return monthList;
        }catch (Exception e){
            throw new BaseException(MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @Override
    public List<Integer> GetNowMonthsUserNum() {
        try{
            List<Integer> monthUserNumList = new ArrayList<>();

            List<LocalDate> months = new ArrayList<>();

            LocalDate monthbeig = LocalDate.now().plusMonths(-12);
            LocalDate monthend = LocalDate.now();
            while (!monthbeig.equals(monthend)){
                months.add(monthbeig.with(TemporalAdjusters.lastDayOfMonth()));
                monthbeig = monthbeig.plusMonths(1);
            }
            for (LocalDate month : months) {
                System.out.println(month);
                Integer monthUserNum = workloadstatMapper.getEvryMonthUserNum(month);
                monthUserNumList.add(monthUserNum);
            }
            return monthUserNumList;
        }catch (Exception e){
            throw new BaseException(MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return workloadstatMapper.findSetmealCount();
    }

    @Override
    public Map<String, Object> getBusinessReport() {
        try{
            //获取当前日期
            LocalDate today = LocalDate.now();
            //获得本周一的日期
            LocalDate thisWeekMonday = today.minusWeeks(1).with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
            //获得本月第一天的日期
            LocalDate firstDay4ThisMonth = today.with(TemporalAdjusters.firstDayOfMonth());
            //今日新增会员数
            Integer todayNewMember = workloadstatMapper.findMemberCountByDate(today);
            //总会员数
            Integer totalMember = workloadstatMapper.getEvryMonthUserNum(LocalDate.now());
            //本周新增会员数
            Integer thisWeekNewMember = workloadstatMapper.findMemberCountAfterDate(thisWeekMonday);
            //本月新增会员数
            Integer thisMonthNewMember = workloadstatMapper.findMemberCountAfterDate(firstDay4ThisMonth);
            //今日预约数
            Integer todayOrderNumber = orderMapper.findOrderCountByDate(today);
            //本周预约数
            Integer thisWeekOrderNumber = orderMapper.findOrderCountAfterDate(thisWeekMonday);
            //本月预约数
            Integer thisMonthOrderNumber = orderMapper.findOrderCountAfterDate(firstDay4ThisMonth);
            //今日到诊数
            Integer todayVisitsNumber = orderMapper.findVisitsCountByDate(today);

            //本周到诊数
            Integer thisWeekVisitsNumber = orderMapper.findVisitsCountAfterDate(thisWeekMonday);

            //本月到诊数
            Integer thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate(firstDay4ThisMonth);

            //热门套餐（取前4）
            List<Map> hotSetmeal = orderMapper.findHotSetmeal();

            Map<String,Object> result = new HashMap<>();
            result.put("reportDate",today);
            result.put("todayNewMember",todayNewMember);
            result.put("totalMember",totalMember);
            result.put("thisWeekNewMember",thisWeekNewMember);
            result.put("thisMonthNewMember",thisMonthNewMember);
            result.put("todayOrderNumber",todayOrderNumber);
            result.put("thisWeekOrderNumber",thisWeekOrderNumber);
            result.put("thisMonthOrderNumber",thisMonthOrderNumber);
            result.put("todayVisitsNumber",todayVisitsNumber);
            result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
            result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
            result.put("hotSetmeal",hotSetmeal);

            return result;
        }catch (Exception e){
            throw new BaseException(MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
